package com.leyou.goods.service;

import com.leyou.goods.client.BrandClient;
import com.leyou.goods.client.CategoryClient;
import com.leyou.goods.client.GoodsClient;
import com.leyou.goods.client.SpecClient;
import com.leyou.item.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description:
 * @author: Mr_Sun
 * @Date: 2018/9/20
 * @Time: 15:07
 */
@Service
public class GoodsService {
    @Autowired
    private BrandClient brandClient;
    @Autowired
    private CategoryClient categoryClient;
    @Autowired
    private GoodsClient goodsClient;
    @Autowired
    private SpecClient specClient;
    public Map<String, Object> loadData(Long id) {
        Map<String, Object> modelMap = new HashMap<>();
        //查询spu
        Spu spu = this.goodsClient.querySpuBySpuId(id);
        //查询spuDetail
        SpuDetail spuDetail = this.goodsClient.querySpuDetailById(id);
        //查询品牌
        Brand brand = this.brandClient.queryBrandByBid(spu.getBrandId());
        //初始化一个map
        List<Map<String, Object>> categories = new ArrayList<>();
        //根据分类的ids   查询names
        List<Long> cids = Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3());
        List<String> names = this.categoryClient.queryNameByIds(cids);
        for (int i = 0; i < cids.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", cids.get(i));
            map.put("name", names.get(i));
            categories.add(map);
        }
        //查询spu下的所有sku
        List<Sku> skus = this.goodsClient.querySkuBySpuId(id);
        //查询groups
        List<SpecGroup> groups = this.specClient.queryGroupWithParam(spu.getCid3());
        //查询特有的规格参数
        List<SpecParam> params = this.specClient.querySpecParam(null, spu.getCid3(), null, false);
        //初始化特有规格参数的map
        Map<Long, String> paramMap = new HashMap<>();
        params.forEach(param->{
            paramMap.put(param.getId(), param.getName());
        });
        modelMap.put("spu", spu);
        modelMap.put("spuDetail", spuDetail);
        modelMap.put("brand", brand);
        modelMap.put("categories", categories);
        modelMap.put("skus", skus);
        modelMap.put("groups", groups);
        modelMap.put("paramMap", paramMap);
        return modelMap;
    }
}
